import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home/index.vue'),
    meta: {
      title: '首页',
    },
  },
  {
    path: '/wxlogin',
    name: 'wxlogin',
    component: () => import('@/views/WXlogin/index.vue'),
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('@/views/Detail/index.vue'),
    redirect: '/detail/register',
    children: [
      {
        path: 'register',
        name: 'Register',
        component: () => import('@/views/Detail/Register/index.vue'),
        meta: {
          title: '预约挂号',
        },
      },
      {
        path: 'particulars',
        name: 'Particulars',
        component: () => import('@/views/Detail/Particulars/index.vue'),
        meta: {
          title: '医院详情',
        },
      },
      {
        path: 'inform',
        name: 'Inform',
        component: () => import('@/views/Detail/Inform/index.vue'),
        meta: {
          title: '预约通知',
        },
      },
      {
        path: 'inquire',
        name: 'Inquire',
        component: () => import('@/views/Detail/Inquire/index.vue'),
        meta: {
          title: '查询/取消',
        },
      },
      {
        path: 'suspend',
        name: 'Suspend',
        component: () => import('@/views/Detail/Suspend/index.vue'),
        meta: {
          title: '停诊信息',
        },
      },
      {
        path: 'register_first_step',
        name: 'RegisterFirstStep',
        component: () => import('@/views/Detail/Register/Register_firstStep.vue'),
        meta: {
          title: '预约第一步',
        },
      },
      {
        path: 'register_next_step',
        name: 'RegisterNextStep',
        component: () => import('@/views/Detail/Register/Register_nextStep.vue'),
        meta: {
          title: '预约第二步',
        },
      },
    ],
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('@/views/User/index.vue'),
    redirect: '/user/autonym',
    children: [
      {
        path: 'autonym',
        name: 'Autonym',
        component: () => import('@/views/User/Autonym/index.vue'),
        meta: {
          title: '实名认证',
        },
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import('@/views/User/Order/index.vue'),
        meta: {
          title: '订单详情',
        },
      },
      {
        path: 'patient',
        name: 'Patient',
        component: () => import('@/views/User/Patient/index.vue'),
        redirect: '/user/patient/patient_list',
        children: [
          {
            path: 'patient_list',
            name: 'PatientList',
            component: () => import('@/views/User/Patient/components/PatientList.vue'),
            meta: {
              title: '就诊人管理',
            },
          },
          {
            path: 'a_u_patient',
            name: 'AUPatient',
            component: () => import('@/views/User/Patient/components/AUPatient.vue'),
            meta: {
              title: '添加/修改就诊人',
            },
          },
        ],
      },
      {
        path: 'account',
        name: 'Account',
        component: () => import('@/views/User/Account/index.vue'),
        meta: {
          title: '账号信息',
        },
      },
      {
        path: 'feedback',
        name: 'Feedback',
        component: () => import('@/views/User/Feedback/index.vue'),
        meta: {
          title: '意见反馈',
        },
      },
    ],
  },
]

const router = createRouter({
  // 路由的模式
  history: createWebHistory(),
  routes,
  scrollBehavior: () => {
    // 始终滚动到顶部
    return {
      top: 0,
    }
  },
})

import { useUserStore } from '@/store/modules/userStore'
router.beforeEach((to, _from, next) => {
  const userStore = useUserStore()
  const { token } = userStore.userInfo
  if (token) {
    next()
  } else if (
    to.path === '/home' ||
    to.path === '/detail/inquire' ||
    to.path === '/detail/register' ||
    to.path === '/detail/particulars' ||
    to.path === '/detail/inform' ||
    to.path === '/detail/suspend'
  ) {
    next()
  } else {
    userStore.visiable = true
    next('/home')
  }
})

router.afterEach((to) => {
  document.title = (to.meta.title as string) || '在线医疗服务平台'
})

export default router
